/* 
 * File:   Subject.cpp
 * Author: ivar
 * 
 * Created on June 13, 2010, 5:32 PM
 */

#include "headers/Subject.h"
#include "headers/Observer.h"
#include <algorithm>

using namespace std;

Subject::Subject() {
    mObservers = new vector<Observer*>();
}

Subject::Subject(const Subject& orig) {
}

Subject::~Subject() {
    mObservers->clear();
    
}

void Subject::attach(Observer *o) {
    /*
     * This makes sure that the same observer does not get added twice
     */
    if(find(mObservers->begin(), mObservers->end(), o) == mObservers->end() &&
            mObservers->back() != o) {
        mObservers->push_back(o);
    }
}

void Subject::detach(Observer *o) {
    mObservers->erase(find(mObservers->begin(), mObservers->end(), o));
}

void Subject::detachAll() {    
    mObservers->clear();
}

void Subject::notify() {
    for(int i=0; i<= mObservers->size(); i++) {
        mObservers->at(i)->update(this);
    }

}

